home *** CD-ROM | disk | FTP | other *** search
- Path: research.nokia.com!news
- From: Johan Wikman <johan.wikman@research.nokia.com>
- Newsgroups: comp.lang.c++,comp.object
- Subject: Re: Style question: OOD & initialisation
- Date: Sun, 17 Mar 1996 19:35:08 +0200
- Organization: Nokia Research Center
- Message-ID: <314C4D4C.51B4@research.nokia.com>
- References: <MARTING.96Mar13154718@igly.jtec.com.au> <RMARTIN.96Mar14120844@rcm.oma.com>
- NNTP-Posting-Host: nrcpb-127.research.nokia.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Robert C. Martin wrote:
- >
- > In article <MARTING.96Mar13154718@igly.jtec.com.au> marting@jtec.com.au (Martin Gregory) writes:
- >
- > main()
- > {
- > Controller TheController;
- > }
- >
- > However, is this concept of a constructor that doesn't exit till the
- > end of time a bit of a worry? (I think it is at least valid.)
- >
- > Would this type of thing be preferrable:
- >
- > main()
- > {
- > Controller TheController;
- >
- > TheController.Go();
- > }
- >
- > If so, why?
- >
- > Because it allows the following:
- >
- > main()
- > {
- > Controller c1(1), c2(2);
- > if (/* some test */)
- > c1.go();
- > else
- > c2.go();
- > };
-
- You could just as well have:
-
- main()
- {
- if (/* some test* /)
- {
- Controller c1(1);
- }
- else
- {
- Controller c2(2);
- }
- }
-
- in which case you would also only create *one* Controller object (which
- is what you want) instead of two.
-
- I prefer the go-version of three reasons:
-
- 1) I think a constructor should only initialize an object and do
- nothing else.
-
- 2) If the constructor does the lot, it effectively means that you can
- not derive from the class.
-
- 3) It allows you to write
-
- int main()
- {
- Controller c;
-
- return c.go();
- }
-
- which I think is nice.
-
- >
- > --
- > Robert Martin | Design Consulting | Training courses offered:
- > Object Mentor Assoc.| rmartin@oma.com | OOA/D, C++, Advanced OO
- > 14619 N. Somerset Cr| Tel: (847) 918-1004 | Mgt. Overview of OOT
- > Green Oaks IL 60048 | Fax: (847) 918-1023 | http://www.oma.com
-
- --
- johan.wikman@research.nokia.com
-